package com.wxs.shiro.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wxs.common.pojo.Resource;
import com.wxs.common.service.ResourceService;
import com.wxs.common.utils.DataResult;
import com.wxs.common.utils.JsonResult;

/**
 * 菜单权限
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/resource/")
public class ResourceController {
	@Autowired
	private ResourceService resourceService;

	@RequestMapping("main.jhtml")
	public ModelAndView main() {
		return new ModelAndView("resource/list");
	}

	@ResponseBody
	@RequestMapping("list.jhtml")
	public DataResult list() {
		List<Resource> list = resourceService.listAll();
		DataResult dr = new DataResult(list, (long) list.size());
		return dr;
	}

	@RequestMapping("add.jhtml")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("resource/add");
		List<Resource> list = resourceService.treeList();
		mv.addObject("resourceList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveAdd.jhtml", produces = "text/html;charset=UTF-8")
	public String saveAdd(Resource resource) {
		try {
			resource.setId(null);
			int c = resourceService.saveResource(resource);
			System.out.println(c);
			return JsonResult.toSuccess(null, "保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(null, "保存失败！");
		}
	}

	@RequestMapping("edit.jhtml")
	public ModelAndView edit(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mv = new ModelAndView("resource/edit");
		Resource r = resourceService.findById(id);
		mv.addObject("resource", r);
		List<Resource> list = resourceService.treeList();
		mv.addObject("resourceList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveEdit.jhtml", produces = "text/html;charset=UTF-8")
	public String saveEdit(Resource Resource) {
		try {
			if (resourceService.editResource(Resource) > 0) {
				return JsonResult.toSuccess(null, "修改成功！");
			} else {
				return JsonResult.toError(null, "修改失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(null, "修改失败！原因：" + e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("doCheckName")
	public Boolean checkName(@RequestParam(value = "name", required = true) String name, Integer id) {
		return resourceService.checkName(name, id);
	}

	@ResponseBody
	@RequestMapping(value = "doDeleteRole", produces = "text/html;charset=UTF-8")
	public String doDeleteRole(@RequestParam(value = "id", required = true) Integer id) {
		try {
			resourceService.deleteById(id);
			return JsonResult.toSuccess(null, "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(null, "删除失败！原因是" + e.getMessage());
		}
	}
}
